<template>
  <el-dialog :visible.sync="visible" :title="dialogTitle">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item label="插件名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="插件类型" prop="type">
        <el-select v-model="formData.type" placeholder="请选择插件类型">
          <el-option v-for="type in pluginTypes" :key="type.value" :label="type.label" :value="type.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="配置" prop="config">
        <el-input v-model="formData.config" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="代码" prop="code">
        <CodeEditor v-model="formData.code" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import CodeEditor from './CodeEditor.vue';

export default {
  props: ['visible', 'dialogTitle', 'formData', 'pluginTypes'],
  components: { CodeEditor },
  data() {
    return {
      formRules: {
        name: [
          { required: true, message: '请输入插件名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择插件类型', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入代码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:visible', false);
    },
    submit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$emit('submit', { ...this.formData });
          this.$message.success('操作成功')
          this.closeDialog();
        } else {
          console.log('表单验证失败!');
          return false;
        }
      });
    }
  }
};
</script>
